<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Dao;

use App\Kernel\Traits\DaoTrait;
use App\Model\IntegralDetail;

/**
 * 积分明细 Dao
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Dao
 */
class IntegralDetailDao extends Dao
{
    use DaoTrait;

    public function model(): string
    {
        return IntegralDetail::class;
    }

    /**
     * 积分状态：消费积分
     *
     * @var int
     */
    const CONSUME_INTEGRAL = 1;


    /**
     * 积分状态：分享积分
     *
     * @var int
     */
    const SHARE_INTEGRAL = 2;

    /**
     * 积分类型：收入积分
     *
     * @var int
     */
    const INCOME_TYPE = 1;

    /**
     * 积分类型：支出积分
     *
     * @var int
     */
    const CONSUME_TYPE = 2;

    /**
     * 积分列表
     * @param array $map
     * @param int $first
     * @param int $last
     * @return mixed
     */
    public function integral(array $map, int $first, int $last)
    {
        return IntegralDetail::query()
            ->where(function($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->whereBetween('create_time', [$first, $last])
            ->paginate(10);
    }

    /**
     * 积分列表 - 分润
     * @param array $map
     * @return mixed
     */
    public function getByUserId(array $map = [])
    {
        return IntegralDetail::query()
            ->with('user:id,avatar,name,phone')
            //->with('user_coupon:id,price')
            ->where(function($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->orderByDesc('create_time')
            ->orderBy('id')
            //->select('id', 'num', 'user_id', 'lower_id', 'order_no')
            ->paginate(10);
    }

    /**
     * 积分列表
     * @param array $map
     * @return mixed
     */
    public function get(array $map = [])
    {
        return IntegralDetail::query()
            ->where(function($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->orderByDesc('create_time')
            ->orderBy('id')
            ->select('num', 'remark', 'create_time')
            ->paginate(10);
    }

    /**
     * 通过用户ID 计算积分总额
     * @param array $map
     * @return mixed
     */
    public function sumIntegralBalance(array $map)
    {
        return IntegralDetail::query()
            ->where(function($query) use ($map) {
                return queryWhere($query, $map);
            })
        ->sum('num');
    }

    /**
     * create
     * @param array $data
     * @return mixed
     */
    public function create(array $data)
    {
        return IntegralDetail::query()->create($data);
    }


    /**
     * 获取积分详情列表通过map
     * @param array $map
     * @param string|null $sort
     * @param array|null $select
     * @param int|null $paginate
     * @return mixed
     */
    public function getIntegralDetailsMap(array $map = [], string $sort = null,array $select = null, int $paginate = null)
    {
        $model = self::makeModel();

        if (isset($map['between_field'])) {
            foreach ($map['between_field'] as $k => $value) {
                $model = $model->whereBetween($k, $value);
            }
            unset($map['between_field']);
        }

        if (!empty($map)) {
            $model = $model->where(function ($query) use ($map) {
                return queryWhere($query, $map);
            });
        }
        if (null !== $sort) {
            $model = $model->orderByRaw($sort);
        }

        if (null !== $select) {
            $model = $model->select($select);
        }

        return $paginate > 0 ? $model->paginate($paginate) : $model->get();
    }

    /**
     * 获取消费积分数量通过用户ID数组 分组
     * @param array $user_ids
     * @param string $select_raw
     * @param string|array $group_by
     * @return mixed
     */
    public function getIntegralCountByUserIds(array $user_ids, string $select_raw, $group_by)
    {
        return IntegralDetail::query()
            ->whereIn('user_id', $user_ids)
            ->selectRaw($select_raw)
            ->groupBy($group_by)
            ->get();
    }

}